package com.zhl.media.commonres.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zhl.media.commonres.R;
import com.zhl.media.commonres.view.HorizontalProgressBar;

public class ProgressDialog extends Dialog {
    private TextView title,des,tv;
    private ImageView over;
    private LinearLayout cancelLay;
    private ProgressBar horizontalProgressBar;
    private String mTitle;
    public ProgressDialog(@NonNull Context context,String title) {
        super(context);
        this.mTitle = title;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_progress, null);
        title = view.findViewById(R.id.title);
        cancelLay = view.findViewById(R.id.cancelLay);
        tv = view.findViewById(R.id.tv);
        title.setText(mTitle);
        des = view.findViewById(R.id.des);
        horizontalProgressBar = view.findViewById(R.id.horizontalProgressBar);
        over = view.findViewById(R.id.over);
        setContentView(view);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(lp);
        setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                return true;
            }
        });
        cancelLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.close();
            }
        });
    }

    public void setComplete() {
        horizontalProgressBar.setVisibility(View.GONE);
        over.setVisibility(View.VISIBLE);
    }

    public void setMessage(String s) {
        des.setText(s);
    }

    public void setCloseText(String ok) {
        tv.setText(ok);
    }


    HallListener listener;

    public void setListener(HallListener listener) {
        this.listener = listener;
    }

    public interface HallListener {
        void close();
    }

}
